import common from '@ohos.app.ability.common';

// 构造GlobalContext单例对象,UIAbilityContext保存在其中
export class GlobalContext {

  private static instance: GlobalContext;
  private static _objects = new Map<string, Object>();

  private constructor() {}
  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  public static getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }

  public static setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }

  public static getUIAbilityContext(): common.UIAbilityContext {
    let context = this._objects.get(GlobalContextConstants.UI_ABILITY_CONTEXT);
    return context as common.UIAbilityContext
  }

  public static setUIAbilityContext(context: common.UIAbilityContext): void {
    this._objects.set(GlobalContextConstants.UI_ABILITY_CONTEXT, context);
  }
}

export class GlobalContextConstants {
  static readonly UI_ABILITY_CONTEXT = "UIAbilityContext";
  static readonly UI_ABILITY_WANT = "UIAbilityWant";
  static readonly UI_HELPER = "UIHelper";
}